﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class QuyenHT_Ser
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public QuyenHT_Ser()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public decimal AddUpdate(QUYENHT entity)
        {
            try
            {
                if (entity.QUYENHTID == 0) dbContext.QUYENHT.Add(entity);
                else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return entity.QUYENHTID;
            }
            catch
            {
                return 0;
            }
        }

        public List<QUYENHT> getList()
        {
            try
            {
                return dbContext.QUYENHT.ToList();
            }
            catch
            {
                List<QUYENHT> _list = new List<QUYENHT>();
                return _list;
            }
        }

        public QUYENHT GetById(int Id)
        {
            try
            {
                dbContext.Configuration.ProxyCreationEnabled = false;
                return dbContext.QUYENHT.Where(p => p.QUYENHTID == Id).First();
            }
            catch
            {
                QUYENHT _list = new QUYENHT();
                return _list;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                var _Entitys = GetById(id);
                if (_Entitys.QUYENHTID != null)
                {
                    dbContext.QUYENHT.Remove(_Entitys);
                }
                dbContext.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool CheckUsetOnQuyenHT(int User, int QuyenHT)
        {
            try
            {
                var c = dbContext.USER_QUYENHT.Where(p => p.USERID == User && p.QUYENHTID == QuyenHT).First();
                if (c != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch { return false; }
        }
    }
}
